package com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.http.request.LogoutRequest;
import com.example.win.android_banduyujia.http.response.BaseHttpResponse;
import com.example.win.android_banduyujia.login.application.LoginApplication;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.settingsactivity.AboutActivity;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.ChangePwdActivity;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.firmactivity.RemindActivity;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseTitleBarActivity;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;

/**
 * Created by huchun on 2015/8/15.
 */
public class SettingsActivity extends BaseTitleBarActivity implements View.OnClickListener {
    private static final String TAG = SettingsActivity.class.getSimpleName();

    private TextView   mTxtSetup, mTxtMe, mTxtPush, mTxtFixPassword, mTxtLinear;
    private Button     mBtnLogOut;

    private Context  context;
    private String   mCover; // 图片地址

    private LoginApplication mLoginApplication;

    private SocializeListeners.SnsPostListener mSnsPostListener;
    // 整个平台的Controller,负责管理整个SDK的配置、操作等处理

    final static com.umeng.socialize.controller.UMSocialService mController =
            (com.umeng.socialize.controller.UMSocialService) UMServiceFactory.getUMSocialService("umeng.share");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_settings);

        mLoginApplication = LoginApplication.getInstance();

        com.umeng.socialize.utils.Log.LOG = true;
        InitTitles();
        InitViews();
    }

    private void InitTitles() {
        setTitle(R.string.text_view_settings);
        showLeftBackButton();
    }

    private void InitViews() {
        mTxtSetup = (TextView) findViewById(R.id.title_list_item_course_reminder_setup);
        mTxtMe = (TextView) findViewById(R.id.title_list_item_me);
        mTxtPush = (TextView) findViewById(R.id.title_list_item_push);
        mTxtFixPassword = (TextView) findViewById(R.id.title_list_item_fix_password);
        mTxtLinear = (TextView) findViewById(R.id.title_list_item_liner);
        mBtnLogOut = (Button) findViewById(R.id.layout_button_data_log_out);

        mTxtSetup.setOnClickListener(this);
        mTxtMe.setOnClickListener(this);

        mTxtFixPassword.setOnClickListener(this);
        mTxtLinear.setOnClickListener(this);
        mBtnLogOut.setOnClickListener(this);

        mTxtPush.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new popupwindows(SettingsActivity.this, mTxtPush);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_list_item_course_reminder_setup: // 课程提醒设置
                 ReminderSetUpActivity();
                break;
            case R.id.title_list_item_me: // 关于我们
                 MeActivity();
                break;
            /*case R.id.title_list_item_push: // 推荐教练APP给朋友
                setShare();
                break;*/
            case R.id.title_list_item_fix_password:  // 修改密码
                 gotoChangePwdActivity();
                break;
            case R.id.title_list_item_liner: // 客服热线
                 LinerActivity();
                break;
            case R.id.layout_button_data_log_out:   // 直接退出登录
                logout();
                // startActivity(new Intent(this, LoginActivity.class));
                break;
        }
    }

    private void logout() {
         LogoutRequest request = new LogoutRequest(LoginApplication.getInstance().isLogin());
         mAsycHttpRequest.doRequest(this, request, BaseHttpResponse.class, mLogoutHandler, true);
    }

    private Handler mLogoutHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            showToast("退出成功");
            finish();
        }
    };

    private void ReminderSetUpActivity() {
        startActivity(new Intent(this, RemindActivity.class));
    }

    private void MeActivity() {
        startActivity(new Intent(this, AboutActivity.class));
    }

    private void gotoChangePwdActivity() {
         startActivity(new Intent(this, ChangePwdActivity.class));
    }

    private void LinerActivity() {
           Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "4008008238"));
           startActivity(intent);
    }

    public class popupwindows extends PopupWindow{

        public popupwindows(Context mContext, View parent) {
            View view = View.inflate(mContext, R.layout.view_share_page, null);
            view.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_ins));
            LinearLayout ll_popup = (LinearLayout) view.findViewById(R.id.share_popup);
            ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_bottom_in_2));

            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setBackgroundDrawable(new BitmapDrawable());
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(view);
            showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            update();

            final LinearLayout layout1= (LinearLayout) view.findViewById(R.id.layout_wechat);
            LinearLayout layout2= (LinearLayout) view.findViewById(R.id.layout_wecircle);
            LinearLayout layout3= (LinearLayout) view.findViewById(R.id.layout_sina);
            LinearLayout layout4= (LinearLayout) view.findViewById(R.id.layout_qzone);

            layout1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 添加新浪sso授权
                   // setShare();
                }
            });
            layout2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   // setShare();
                }
            });
            layout3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   // setShare();
                }
            });
            layout4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   // setShare();
                }
            });
        }
    }

    /**
     * @功能描述 : 存放皮质的方法
     * @return
     */
    private void setShare() {
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        // 添加微信、微信朋友圈平台
        addWXPlatform();
        mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QZONE);
        mController.openShare((Activity) context,  false);
    }

    /**
     * @功能描述 : 添加微信平台分享
     * @return
     */
    private void addWXPlatform() {
         // 注意：在微信授权的时候，必须传递appSecret
         // wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
         String appId = "wxd33c08e622a126a4";
         String appSecret = "a46884e2aaaf1c5909ac8ab9c583d81d";

         // 添加微信平台
        UMWXHandler  wxHandler = new UMWXHandler(context, appId, appSecret);
        wxHandler.addToSocialSDK();

        // 支持微信朋友圈
        UMWXHandler  umwxHandler = new UMWXHandler(context, appId, appSecret);
        umwxHandler.setToCircle(true);
        umwxHandler.addToSocialSDK();

        /**
         * 添加QZone在分享列表页中
         */
        //参数1为当前Activity，参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(this, "100424468", "c7394704798a158208a74ab60104f0ba");
        qZoneSsoHandler.addToSocialSDK();
        /**
        * 注册微信回调方法
        */
        mSnsPostListener = new SocializeListeners.SnsPostListener() {
            @Override
            public void onStart() { }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, SocializeEntity socializeEntity) {
                  if(i == 200){
                      Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT).show();
                  }else{
                      Toast.makeText(context, "分享失败: error code :" +i, Toast.LENGTH_SHORT).show();
                  }
             }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
